<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="utf-8">
    <meta name="robots" content="noindex">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href="../public.css" rel="stylesheet">
    <link crossorigin="anonymous"
        integrity="sha512-tH5CEoO7QorGJK5RZrcKozo5xe0qC0UgOhjkIoqXNVe9ApFAjJRbVFzDfVvdRUSOJ5g2Pw9VzzOBguouzhLoIQ=="
        href="https://lib.baomitu.com/material-components-web/6.0.0/material-components-web.min.css" rel="stylesheet">
    <script crossorigin="anonymous"
        integrity="sha512-5zCl3JhN4Fqq6+irTX1v8J+77hwL54zTbrdl2Dl8YHe+KGcuV14C01u/uWFrSg+kZgOfGMneoUySVHqEgdRaPQ=="
        src="https://lib.baomitu.com/material-components-web/6.0.0/material-components-web.min.js"></script>
    <link crossorigin="anonymous"
        href="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/material-design-icons/3.0.2/iconfont/material-icons.min.css"
        rel="stylesheet">
</head>

<body style="text-align: center">
    <button class="mdc-button mdc-button--raised mdc-button--leading" style="width:80%;margin:16px;" id="speedBtn">
        <span class="mdc-button__ripple"></span>
        <i class="material-icons mdc-button__icon" aria-hidden="true">network_check</i>
        <span class="mdc-button__label" id="speedBtnLabel">测速</span>
    </button>
    <br>
    <button class="mdc-button mdc-button--raised mdc-button--leading" style="width:80%;margin:16px;display: none;"
        id="cfBtn">
        <span class="mdc-button__ripple"></span>
        <i class="material-icons mdc-button__icon" aria-hidden="true">network_check</i>
        <span class="mdc-button__label">Cloudflare诊断</span>
    </button>
    <br>
    <div class="mdc-form-field">
        <div class="mdc-checkbox">
            <input type="checkbox" class="mdc-checkbox__native-control" id="otherDomain" />
            <div class="mdc-checkbox__background">
                <svg class="mdc-checkbox__checkmark" viewBox="0 0 24 24">
                    <path class="mdc-checkbox__checkmark-path" fill="none" d="M1.73,12.91 8.1,19.28 22.79,4.59" />
                </svg>
                <div class="mdc-checkbox__mixedmark"></div>
            </div>
            <div class="mdc-checkbox__ripple"></div>
        </div>
        <label for="programCache">测试其他可用节点</label>
    </div>
    <br>
    <div id="cfTrace"></div>
    <H4>程序节点</H4>
    <div id="mainDomain">
    </div>
    <br>
    <H4>音频节点</H4>
    <div id="cdnDomain">
    </div>
    <script>
        window.mdc.autoInit();
        window.MDCCheckbox = mdc.checkbox.MDCCheckbox;
        document.querySelectorAll('.mdc-button').forEach(element => {
            mdc.ripple.MDCRipple.attachTo(element);
        });
        document.querySelectorAll('.mdc-checkbox').forEach(element => {
            new MDCCheckbox(element);
        });
        let domain = (location.hostname == "127.0.0.1") ? "545.qinlili.bid" : location.host;
        if (domain == "545.qinlili.bid") {
            document.getElementById("cfBtn").style.display = "";
            document.getElementById("cfBtn").addEventListener("click", async () => {
                let cfTrace = (await (await fetch("https://" + domain + "/cdn-cgi/trace?ForceNoCache=1")).text()).split("\n");
                console.log(cfTrace);
                document.getElementById('cfTrace').innerHTML = "";
                cfTrace.forEach(line => {
                    let text = document.createElement("p");
                    text.innerText = line.replace("=", ":");
                    document.getElementById('cfTrace').appendChild(text);
                })
            })
        }
        document.getElementById("speedBtn").addEventListener("click", async () => {
            document.getElementById('cfTrace').innerHTML = "";
            document.getElementById("speedBtn").disabled = true;
            document.getElementById("speedBtnLabel").innerText = "正在测速...";
            document.getElementById("mainDomain").innerHTML = "";
            document.getElementById("cdnDomain").innerHTML = "";
            let domains = [];
            if (document.getElementById("otherDomain").checked) {
                domains = ['545.qinlili.bid', 'ipv6.545.qinlili.bid', '545webplayer.pages.dev', '545.github.qinlili.bid', '545.do.qinlili.bid', 'shengge-player-vfoza.ondigitalocean.app']
            } else {
                domains = [(location.hostname == "127.0.0.1") ? "545.qinlili.bid" : location.host]
            }
            let cdnDomains = [{
                name: "雨课堂",
                domain: "bj.bcebos.com",
                file: "https://bj.bcebos.com/yunpanx/%E6%B5%B7%E6%B5%B7%E6%B5%B7_20220415194656_23666.ogg",
                length: 5892464
            }, {
                name: "Cloudflare",
                domain: "cdn-cf.545.qinlili.bid",
                file: "https://cdn-cf.545.qinlili.bid/%E8%87%AA%E4%BC%A0/%E5%A4%A7%E5%96%9C.ogg",
                length: 4674297
            }];
            for (let i = 0; domains[i]; i++) {
                let info = document.createElement("p");
                let canuse = true;
                document.getElementById("mainDomain").appendChild(info);
                info.innerText = domains[i] + "-连接中...";
                await fetch("https://" + domains[i] + "/?ForceNoCache=1", { method: "HEAD", mode: "no-cors" }).catch(() => {
                    info.innerText = domains[i] + "-连接失败";
                    info.style.color = "red";
                    canuse = false;
                })
                if (canuse) {
                    info.innerText = domains[i] + "-测试延迟...";
                    let startTime = Date.now();
                    await (await fetch("https://" + domains[i] + "/?ForceNoCache=1&random=" + Math.random(), { method: "HEAD", mode: "no-cors" }).catch(() => {
                        info.innerText = domains[i] + "-延迟测试失败";
                        info.style.color = "red";
                        canuse = false;
                    })).blob().catch(() => {
                        info.innerText = domains[i] + "-延迟测试失败";
                        info.style.color = "red";
                        canuse = false;
                    })
                    let latency = Date.now() - startTime;
                    info.innerText = domains[i] + "-" + latency + "MS-测试速度...";
                    if (canuse) {
                        let startTime = Date.now();
                        await (await fetch("https://" + domains[i] + "/img/%E5%A4%A7%E5%A1%94.webp?ForceNoCache=1&random=" + Math.random(), { headers: { "cache-control": "no-cache" }, method: "GET", mode: "no-cors" }).catch(() => {
                            info.innerText = domains[i] + "-" + latency + "MS-速度测试失败";
                            info.style.color = "red";
                            canuse = false;
                        })).blob().catch(() => {
                            info.innerText = domains[i] + "-" + latency + "MS-速度测试失败";
                            info.style.color = "red";
                            canuse = false;
                        })
                        let time = Date.now() - startTime;
                        info.innerText = domains[i] + "-" + latency + "MS-" + (400770 / time).toFixed(2) + "KB/S";
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
            };

            for (let i = 0; cdnDomains[i]; i++) {
                let info = document.createElement("p");
                let canuse = true;
                document.getElementById("cdnDomain").appendChild(info);
                info.innerText = cdnDomains[i].name + "-连接中...";
                await fetch("https://" + cdnDomains[i].domain + "/?ForceNoCache=1", { method: "HEAD", mode: "no-cors" }).catch(() => {
                    info.innerText = cdnDomains[i].name + "-连接失败";
                    info.style.color = "red";
                    canuse = false;
                })
                if (canuse) {
                    info.innerText = cdnDomains[i].name + "-测试延迟...";
                    let startTime = Date.now();
                    await (await fetch("https://" + cdnDomains[i].domain + "/?ForceNoCache=1&random=" + Math.random(), { method: "HEAD", mode: "no-cors" }).catch(() => {
                        info.innerText = cdnDomains[i].name + "-延迟测试失败";
                        info.style.color = "red";
                        canuse = false;
                    })).blob().catch(() => {
                        info.innerText = cdnDomains[i].name + "-延迟测试失败";
                        info.style.color = "red";
                        canuse = false;
                    })
                    let latency = Date.now() - startTime;
                    info.innerText = cdnDomains[i].name + "-" + latency + "MS-测试速度...";
                    if (canuse) {
                        let startTime = Date.now();
                        await (await fetch(cdnDomains[i].file + "?ForceNoCache=1&random=" + Math.random(), { headers: { "cache-control": "no-cache" }, method: "GET", mode: "no-cors" }).catch(() => {
                            info.innerText = cdnDomains[i].name + "-" + latency + "MS-速度测试失败";
                            info.style.color = "red";
                            canuse = false;
                        })).blob().catch(() => {
                            info.innerText = cdnDomains[i].name + "-" + latency + "MS-速度测试失败";
                            info.style.color = "red";
                            canuse = false;
                        })
                        let time = Date.now() - startTime;
                        info.innerText = cdnDomains[i].name + "-" + latency + "MS-" + (400770 / time).toFixed(2) + "KB/S";
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
            }
            document.getElementById("speedBtn").disabled = false;
            document.getElementById("speedBtnLabel").innerText = "测速";
        })
    </script>
</body>

</html>